package com.app.freshmenapp;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONObject;
import org.w3c.dom.Text;

import java.lang.reflect.Type;
import java.util.List;

public class NewsListActivity extends AppCompatActivity {

    private ListView mListNews;
    private List<Bean.ListBean> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_list);
        initData();
        initView();
        initEvent();
    }

    private void initEvent() {

    }

    private void initView() {
        mListNews = findViewById(R.id.lv_news);

    }

    private void initData() {
        RequestQueue requestQueue = Volley.newRequestQueue(this);
        String url = "http://182.61.26.165:8080/freshmenapp/news/list";
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.POST, url, "", new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject jsonObject) {
               Gson gson = new Gson();
               Type type = new TypeToken<Bean>(){}.getType();
               Bean bean = gson.fromJson(jsonObject.toString(),type);
               list = bean.getList();
                updateView();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError volleyError) {
                Toast.makeText(NewsListActivity.this, "失败", Toast.LENGTH_SHORT).show();
            }
        }
        );
        requestQueue.add(jsonObjectRequest);
    }

    private void updateView() {
        MyAdapter adapter = new MyAdapter();
        mListNews.setAdapter(adapter);
    }

    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            convertView = View.inflate(NewsListActivity.this,R.layout.list_item_layout,null);
            TextView tvtitle = convertView.findViewById(R.id.tv_title);
            tvtitle.setText(list.get(position).getTitle());
            TextView tvdate = convertView.findViewById(R.id.tv_date);
            tvdate.setText(list.get(position).getDate());
            return convertView;
        }
    }
}
